'''
Created on Mar 23, 2013

@author: Petko
'''
from django.views.generic.base import View
from sent.utils import createValidToken, CommonResult, ErrorIds
from sent.utils.result_printer import getPrinter
from sent.model.OpinionDBManager import OpinionDBManager

class DeleteBaseOpinion(View):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        pass
    
    def post(self, *args, **kwargs):
        accessTokenStr = self.request.POST.get('access_token', None)
        opinionId = self.request.POST.get('opinion_id', None)
        
        cResult = CommonResult()
        
        if accessTokenStr and opinionId:
            
            accessToken, error = createValidToken(accessTokenStr, 'bopin_d')
            if accessToken:
                dbMan = OpinionDBManager(False)
                try:
                    dbMan.deleteBaseOpinion(opinionId)
                    dbMan.commitSession()
                except:
                    dbMan.rollbackSession()
                    raise
            else:
                cResult.errorId = ErrorIds.UNAUTHORIZED
                cResult.result = error
        else:
            cResult.errorId = ErrorIds.BADREQUEST
            cResult.result = "Missing parameter"
            
        return getPrinter().printResult(cResult)
    
    